<?php
header('Content-Type:text/html;charset=utf-8');
include_once ('saemysql.class.php');

$statusid = $_GET['statusid'];
$jsonString = "";
$mysql = new SaeMysql();
$sql = "select * from json where statusid = '".$statusid."'";
$data = $mysql->getData( $sql );
if($data == false){
	echo "请求出错，请查询地址<br/>";
        echo "<a href='influence.php'>返回</a>";
	exit();
} else if($data[0]['jsonstring']!=null) {
        //var_dump($data);
        $jsonString = $data[0]['jsonstring'];
        $shareString = $data[0]['sharestring'];
} else {
    $jsonString = $data[0]['jsonstring1'].$data[0]['jsonstring2'];
    $shareString = $data[0]['sharestring'];
}
$mysql->closeDb();

$posterArray = json_decode($jsonString, true);
$user_followers = array();

//var_dump($posterArray);

$explode = array();
foreach($posterArray['children'] as $child){
    //$childCnt = getChildrenCnt($child)+1;
    $values = getChildrenCntBetter($child, array('count'=>0, 'followers'=>0), $user_followers);
    
    $values['followers'] -= $child['followers'];
    $tmp = array('name'=>$child['name'], 'count'=>$values['count'], 'followers'=>$user_followers[$child['name']], 'behind_followers'=>$values['followers']);
    	
    array_push($explode, $tmp);
}

usort($explode, "cmpExplode");


function getChildrenCntBetter($array, $values, &$user_followers){
	$values['count'] += count($array['children']);
	$values['followers'] += $array['followers'];
	$user_followers[$array['name']] = $array['followers'];
	
	for ($i = 0; $i < count($array['children']); $i++){
		$values = getChildrenCntBetter($array['children'][$i], $values, $user_followers);
	}
	
	return $values;
}

function cmpExplode($a, $b){
    if ($a['count'] == $b['count']) {
        return 0;
    }
    return ($a['count'] > $b['count']) ? -1 : 1;
}




?>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>寻找微博引爆点 草根版</title>

<!-- CSS Files -->
<link type="text/css" href="css/base.css" rel="stylesheet" />
<link type="text/css" href="css/Hypertree.css" rel="stylesheet" />
<link type="text/css" href="css/table.css" rel="stylesheet" />

<!--[if IE]><script language="javascript" type="text/javascript" src="js/excanvas.js"></script><![endif]-->

<!-- Jquery Library File -->
<script language="javascript" type="text/javascript" src="js/jquery.js"></script>

<!-- DataTables Library File -->
<script language="javascript" type="text/javascript" src="js/jquery.dataTables.js"></script>

<!-- JIT Library File -->
<script language="javascript" type="text/javascript" src="js/jit.js"></script>

<script type="text/javascript" charset="utf-8">
	$(document).ready(function() {
		$('#details').dataTable( {
			"aaSorting": [[ 2, "desc" ]]
		} );
	} );
</script>

<!-- HyperTree -->
<script language="javascript" type="text/javascript">
var labelType, useGradients, nativeTextSupport, animate;

(function() {
  var ua = navigator.userAgent,
      iStuff = ua.match(/iPhone/i) || ua.match(/iPad/i),
      typeOfCanvas = typeof HTMLCanvasElement,
      nativeCanvasSupport = (typeOfCanvas == 'object' || typeOfCanvas == 'function'),
      textSupport = nativeCanvasSupport 
        && (typeof document.createElement('canvas').getContext('2d').fillText == 'function');
  //I'm setting this based on the fact that ExCanvas provides text support for IE
  //and that as of today iPhone/iPad current text support is lame
  labelType = (!nativeCanvasSupport || (textSupport && !iStuff))? 'Native' : 'HTML';
  nativeTextSupport = labelType == 'Native';
  useGradients = nativeCanvasSupport;
  animate = !(iStuff || !nativeCanvasSupport);
})();



function init(){
    //init data
    var json = <?php echo $jsonString?>;
    var infovis = document.getElementById('infovis');
    //var w = infovis.offsetWidth - 50, h = infovis.offsetHeight - 50;
    var w = 1000, h = 1000;
    //init Hypertree
    var ht = new $jit.Hypertree({
      //id of the visualization container
      injectInto: 'infovis',
      //canvas width and height
      width: w,
      height: h,
      //Change node and edge styles such as
      //color, width and dimensions.
      Node: {
          dim: 3,
          color: "#f00"
      },
      Edge: {
          lineWidth: 1,
          color: "#088"
      },
      onBeforeCompute: function(node){
          //Log.write("centering");
      },
      //Attach event handlers and add text to the
      //labels. This method is only triggered on label
      //creation
      onCreateLabel: function(domElement, node){
          domElement.innerHTML = node.name;
          $jit.util.addEvent(domElement, 'click', function () {
              ht.onClick(node.id, {
                  onComplete: function() {
                      ht.controller.onComplete();
                  }
              });
          });
      },
      //Change node styles when labels are placed
      //or moved.
      onPlaceLabel: function(domElement, node){
          var style = domElement.style;
          style.display = '';
          style.cursor = 'pointer';
          if (node._depth <= 1) {
              style.fontSize = "0.8em";
              style.color = "#555";

          } else if(node._depth == 2){
              style.fontSize = "0.7em";
              style.color = "#555";

          } else {
              style.fontSize = "0.5em";
              style.color = "#555";
          }

          var left = parseInt(style.left);
          var w = domElement.offsetWidth;
          style.left = (left - w / 2) + 'px';
      },
      
      onComplete: function(){
          //Log.write("done");
          
          //Build the right column relations list.
          //This is done by collecting the information (stored in the data property) 
          //for all the nodes adjacent to the centered node.
          var node = ht.graph.getClosestNodeToOrigin("current");
      }
    });
    //load JSON data.
    ht.loadJSON(json);
    //compute positions and plot.
    ht.refresh();
    //end
    ht.controller.onComplete();
}
</script>
</head>

<body onload="init();">
<p>
    <h2>点击红点可以切换中心哦~</h2><br/>
    <a href='influence.php'>返回查询</a>

    <script type="text/javascript" charset="utf-8">
(function(){
  var _w = 142 , _h = 66;
  var param = {
    url:'',
    type:'4',
    count:'1', /**是否显示转发数，1显示(可选)*/
    title: '<?php echo $shareString;?>', /**转发的文字内容(可选，默认为所在页面的title)*/
    pic:'http://graph.sinaapp.com/image/explode.png', /**转发图片的路径(可选)*/
    rnd:new Date().valueOf()
  }
  var temp = [];
  for( var p in param ){
    temp.push(p + '=' + encodeURIComponent( param[p] || '' ) )
  }
  document.write('<iframe allowTransparency="true" frameborder="0" scrolling="no" src="http://hits.sinajs.cn/A1/weiboshare.html?' + temp.join('&') + '" width="'+ _w+'" height="'+_h+'"></iframe>');
})();
</script>
</p>    
<div id="container">


    <div id="infovis"></div>    

</div>

<center>
<table>
<tr>
<td>
<table cellpadding="0" cellspacing="0" border="0" class="display" id="details">
	<thead>
          <tr>
                <th>#</th>
                <th>转发者</th>
                <th>带来转发量</th>
                <th>粉丝总量</th>
                <th>转发者的粉丝</th>
                <th>带来转发用户的粉丝</th>
          </tr>
    </thead>
    <tbody>
          <?php
            $i=1;
            foreach($explode as $e){
              ?>
          <tr>
                <td><?php echo $i;?></td>
                <td><?php echo $e['name'];?></td>
                <td><?php echo $e['count'];?></td>
                <td><?php echo ($e['followers']+$e['behind_followers']);?></td>
                <td><?php echo $e['followers'];?></td>
                <td><?php echo $e['behind_followers'];?></td>
          </tr>
          <?php
               $i++; }
          ?>
    </tbody>
</table>
</td>
<td>



</td>
</tr>

</table>
</center>

<p>
    <a href='influence.php'>返回查询</a>
</p>
<script src="http://s96.cnzz.com/stat.php?id=3764558&web_id=3764558&show=pic" language="JavaScript"></script>
</body>

</html>